import {computed, ref} from "vue";
import guilingfunc from "../util/guiling";
import http from "../request/request"
import {useMessage} from 'naive-ui'

// @ts-ignore
export const openAndClose = (addFunc: Function | null = null, resetFunc: Function = null, titleend = "", emit: any = null) => {
    const showModal = ref(false)    // 显示弹窗
    const editType = ref("")        // 编辑、新建、查看
    const resultText = ref("")      // 交互结果的文字描述
    const title = computed(() => {
        switch (editType.value) {
            case "add":
                return "新建" + titleend
            case "edit":
                return "编辑" + titleend
            case "view":
                return "查看" + titleend
            default:
                return "新建" + titleend
        }
    })
    // 打开弹窗
    const init = (type: "add" | "edit" | "view" | "", data: any = null) => {
        editType.value = type || "add"
        addFunc && addFunc(data,type)
        showModal.value = true
    }
    // 关闭弹窗
    const handleClose = () => {
        emit && emit("close", editType.value)
        showModal.value = false
        resultText.value = ""
        editType.value = ""
        resetFunc && resetFunc()
    }

    // 交互成功
    const getsuccess = (jieguo: string = "") => {
        if (jieguo) {
            resultText.value = jieguo
        }
        editType.value = "success"
        setTimeout(() => {
            handleClose()
        }, 500)
    }
    // 交互失败
    const getfaild = (jieguo: any = null) => {
        if (jieguo) {
            resultText.value = jieguo.toString()
        }
        editType.value = "error"
    }
    const message = useMessage()
    const loading = ref(false)
    return {showModal, editType, resultText, init, handleClose, getsuccess, getfaild, title, http,message,loading}
}

export const useListIndex = () => {
    const formRef = ref<any>(null)    // 搜索表单
    const fenyeref = ref<any>(null)    // 分页组件
    const editref = ref<any>(null)     // 编辑弹窗
    const formValue = ref<any>({})   // 表单数据
    const tableData = ref<any>([])   // 表格数据
    const total = ref<any>(0)    // 数据总数
    const loading = ref<boolean>(false)   // 表格用的loading状态
    const message = useMessage()
    // 点击查询
    const handleSearch = () => {
        fenyeref.value.changeoption({page: 1})
    }
    // 点击重置
    const handleResetForm = () => {
        formValue.value = guilingfunc(formValue.value)
    }
    // 点击查看
    const handleView = (data: any) => {
        editref.value!.init("view", {...data})
    }
    // 点击编辑
    const handleEdit = (data: any) => {
        editref.value!.init("edit", {...data})
    }
    // 点击新建
    const handleCreate = () => {
        editref.value!.init("add", {})
    }
    // 删除后的刷新
    const refreshdelete = (type = "delete") => {
        if (type === "add" || type === "") {
            fenyeref.value.changeoption({page: 1})
        } else if (type === "edit" || type === "view") {
            fenyeref.value.changeoption({})
        } else {
            const page = fenyeref.value.page
            if (page > 1 && tableData.value.length === 1) {
                fenyeref.value.changeoption({page: page - 1})
            }
            fenyeref.value.changeoption({})
        }
    }
    return {
        formRef, fenyeref, editref, formValue, tableData, total, handleSearch,
        handleResetForm, handleView, handleEdit, handleCreate, refreshdelete, http, message,loading
    }
}
